package com.bird.main;

import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

public class GameBarrierLayer {
    File file = new File("src/com/bird/app/score.txt");
    private List<Barrier> barrierList;
    private GameTime gameTime;
    private Random random = new Random();
    private int numberTop;
    private int numberMid;
    private int numberDown;
    private Double P;  //这个是概率
    private Long score;


    public GameBarrierLayer() {
        this.barrierList = new ArrayList<>();
        this.gameTime = new GameTime();
    }

    public void ran() {
        //生成100~500的随机数
        numberDown = random.nextInt(400) + 100;
        numberTop = random.nextInt(400) + 100;
        //生成150~300的随机数
        numberMid = random.nextInt(150) + 150;
        //生成0~1的随机数
        P = random.nextDouble();
        //必须给中间留出最小的空隙,保证可以有人通过
        if (numberTop + numberDown > 400) {
            ran();
        }
    }

    //用于得到文件中数据
    public int getTxt() {
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            int read = Integer.parseInt(in.readLine());
            in.close();
            return read;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //用于存储数据
    public void setTxt(String str) {
        try {
            FileWriter writer = new FileWriter(file);
            writer.write(str);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void logic(Graphics g) {
        //  初始的绘制
        if (barrierList.size() == 0) {
            ran();
            gameTime.begin();   //游戏开始,计时开始
            insert(600, 0, numberTop, 0);
            insert(600, 500 - numberDown, numberDown, 2);

        } else {
            int txtScore = getTxt();
            g.setColor(new Color(0xFF345C));
            g.setFont(new Font("微软雅黑", Font.BOLD, 20));
            g.drawString("得分:" + this.score, 20, 50);
            this.score = gameTime.getTime();
            if (score > txtScore) {
                setTxt(String.valueOf(score));
                g.drawString("最高得分:" + this.score, 20, 100);
            } else {
                g.drawString("最高得分:" + txtScore, 20, 100);
            }
            if (barrierList.get(barrierList.size() - 1).isIn()) {
                ran();
                if (P > 0.66) {
                    insert(600, 150, numberMid, 4);
                } else if (P > 0.33) {
                    insert(600, 0, numberTop, 0);
                    insert(600, 500 - numberDown, numberDown, 2);
                } else {
                    insert(600, 125, 200, 6);
                }
            }
        }
    }

    public void draw(Graphics g, Bird bird) {

        Iterator<Barrier> iterator = barrierList.iterator();
        while (iterator.hasNext()) {
            Barrier barrier = iterator.next();
            if (barrier.getIsVisible()) {
                barrier.draw(g);
            } else {
                /*TODO:使用迭代器Iterator的remove
                使用了 iterator.remove() 方法来移除当前迭代器所指向的元素。
                这个方法是 Iterator 接口的一部分，并且它是设计用来在迭代过程中安全地移除元素的。
                当你调用 iterator.remove() 时，它会移除 iterator.next() 最近一次返回的元素，而且这个操作只能在 next() 方法之后调用一次。*/
                iterator.remove();
                BarrierPool.backPool(barrier);
            }
        }
        collideBird(bird);
        logic(g);
    }


    public void insert(int x, int y, int num, int type) {
        Barrier fromPool = BarrierPool.getFromPool();
        fromPool.setX(x);
        fromPool.setY(y);
        fromPool.setHeight(num);
        fromPool.setType(type);
        fromPool.setVisible(true);
        barrierList.add(fromPool);
    }

    /*
 判断 障碍物 和 小鸟 是否 发生了碰撞
*/
    public boolean collideBird(Bird bird) {
        Rectangle birdRectangle = bird.getRectangle();
        for (int i = 0; i < barrierList.size(); i++) {
            Rectangle barrierRectangle = barrierList.get(i).getRectangle();
            if (barrierRectangle.intersects(birdRectangle)) {  // 这里的 intersects 是 jdk 里面的方法，判断 两者 是否有交集
                System.out.println("撞到了障碍物!");
                bird.life = false;
                return true;
            }
        }
        return false;
    }

    //清空障碍物的池
    public void reStart() {
        barrierList.clear();
    }
}
